package com.tywj.panda.home

import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.tywj.panda.home.HomeAdImageAdapter.BannerViewHolder
import com.tywj.panda.home.entity.BannerEntity
import com.tywj.lib.core.common.extension.load
import com.youth.banner.adapter.BannerAdapter


/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/05/21
 *  滚动轮播图适配器
 * 参考链接：https://github.com/youth5201314/banner
 *     version: 1.0.0
 * </pre>
 */
class HomeAdImageAdapter(listImgRes: List<BannerEntity>) :
    BannerAdapter<BannerEntity, BannerViewHolder>(listImgRes) {
    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val imageView = ImageView(parent.context)
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return BannerViewHolder(imageView)
    }

    override fun onBindView(holder: BannerViewHolder?, data: BannerEntity, position: Int, size: Int) {
        holder?.imageView?.load(data.url, R.drawable.common_rect_gray_f5_radius_8)
    }

    inner class BannerViewHolder(var imageView: ImageView) : RecyclerView.ViewHolder(imageView)
}